package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.AdminLicenseEntity;
import tech.abing.spider.task.model.amac.AdminPunishEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.UuIdPublic;

public class AdminPunishProcessor implements PageProcessor{
	
	private static Logger logger = Logger.getLogger(AdminPunishProcessor.class);
	
	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		if(!request.getUrl().contains("index_")){
			
			// 如果是首页，构造翻页的url压入队列
			// 最多100页，之后的都请求不到
			for(int i = 2; i <= 10; i++){
				String url = request.getUrl() + "index_"+(i - 1)+".html";
				PageRequest req = context.createPageRequest(url, AdminPunishProcessor.class);
				req.setHeaders(request.getHeaders());
				queue.add(req);
			}
		}
		
		String type = doc.select("div.title").get(1).text();
		Elements table = doc.select("div.fl_list > ul > li");
		if(table !=null && table.size()>0){
			for (Element element : table) {
				AdminPunishEntity punish = new AdminPunishEntity();
				punish.setId(UuIdPublic.timeStrongID(5));
				punish.setCreateTime(new Timestamp(System.currentTimeMillis()));
				punish.setTitle(element.select("a").attr("title"));
				String linkUrl = element.select("a").attr("href");
				punish.setLinkUrl(getUrl(request.getUrl(), linkUrl));
				punish.setPostDate(element.select("span").text());
				punish.setUrl(request.getUrl());
				punish.setType(type.trim());
				objectContainer.add(punish);
				
				
				String linkUrls=getUrl(request.getUrl(), linkUrl);
				PageRequest req = context.createPageRequest(linkUrls, AdminPunishDetailProcessor.class);
				queue.add(req);
			}
		}
	}
	
	private String getUrl(String url, String linkUrl) 
	{
		String tmp = url.substring(0, url.lastIndexOf("/") + 1);
		String tmp1 = linkUrl.substring(2, linkUrl.length());
		return tmp + tmp1;
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
